package xtgl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.google.gson.Gson;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;

public class Deptcontroller extends Controller {
	
	public static int pagen=10;
	public static int dept_id=-1;
	
	public void index() {
		dept_id=-1;
		List<Map<String, Object>> primapList = new ArrayList<Map<String, Object>>(); // 创建primaplist用来放权限与list信息
		List<Privilege> pList = Privilege.dao.find("select * from privilege");

		for (int i = 0; i < pList.size(); i++) {
			Map<String, Object> primap = new LinkedHashMap<String, Object>(); // 创建map用来放权限信息
			primap.put("id", pList.get(i).get("privilege_id"));
			primap.put("pId", pList.get(i).get("parent_id"));
			primap.put("name", pList.get(i).get("privilege_name"));
			if (pList.get(i).get("parent_id").equals(0))
				primap.put("isParent", true);
			primapList.add(primap);
		}
		Gson ggson = new Gson();
		String strrList = ggson.toJson(primapList);
		strrList = strrList.replaceAll("\"id\"", "id");
		strrList = strrList.replaceAll("\"pId\"", "pId");
		strrList = strrList.replaceAll("\"name\"", "name");
		setAttr("privilegelist", strrList);

		List<Map<String, Object>> deptList = new ArrayList<Map<String, Object>>();
		List<Department> ldept = Department.dao.find("select * from dept");
		for (int i = 0; i < ldept.size(); i++) {
			Map<String, Object> dep = new HashMap<String, Object>();
			dep.put("id", ldept.get(i).get("dept_id"));
			dep.put("pid", ldept.get(i).get("prent_id"));
			dep.put("name", ldept.get(i).get("dept_name"));
			dep.put("open", "true");
			if (ldept.get(i).getInt("prent_id").equals(0))
				dep.put("isParent", true);
			dep.put("click", "abclick(" + ldept.get(i).getInt("dept_id") + ")");
			deptList.add(dep);
		}
		Gson gson = new Gson();
		String strList = gson.toJson(deptList);
		strList = strList.replaceAll("\"id\"", "id");
		strList = strList.replaceAll("\"pid\"", "pId");
		strList = strList.replaceAll("\"name\"", "name");
		strList = strList.replaceAll("\"click\"", "click");
		strList = strList.replaceAll("\"open\"", "open");
		setAttr("strList", strList);

		setAttr("user", User.dao.find("select * from user"));
		List<Department> dept = Department.dao.find("select * from dept");
		setAttr("privilege", Privilege.dao.getPrivilege());
		setAttr("list", dept);
		render("/xtgl/dept.html");
	}

	public void select() {
		dept_id=getParaToInt("id");
		renderNull();
		
	}
    /*删除部门*/
	public void del() {
		System.out.println("--------------------------------");
		Dept_Privilege.dao.deleteDeptPrivilegeByRoleId(getParaToInt("id"));
		Db.update("delete from dept where dept_id=" + getParaToInt("id"));
		index();
	}

	public void add() {
		Department dep = getModel(Department.class, "dept");
		dep.save();
		Integer deptId = dep.getInt("dept_id");
		String j[] = getPara("privilege_id").split(",");
		for (int i = 0; i < j.length; i++) {
			Dept_Privilege rp = new Dept_Privilege();
			rp.set("dept_id", deptId)
					.set("privilege_id", Integer.valueOf(j[i])).save();
		}
		redirect("/#dept");
	}

	public void upprivi() {
		Integer dept_id = getParaToInt("dept_id");
		String dept_name = getPara("dept_name");
		Dept_Privilege.dao.deleteDeptPrivilegeByRoleId(dept_id);
		Department dept = Department.dao.findById(dept_id);
		dept.set("dept_name", dept_name).update();
		String j[] = getPara("privilege_id").split(",");
		for (int i = 0; i < j.length; i++) {
			Dept_Privilege rp = new Dept_Privilege();
			rp.set("dept_id", dept_id)
					.set("privilege_id", Integer.valueOf(j[i])).save();
		}
		redirect("/#dept");
	}

	public void pagedemo() {
		List<Object> listrol = new ArrayList<Object>();
		Map<String, Object> map = new HashMap<String, Object>();
		List<Role> rolelist = Role.dao.find("select * from role");
		int n = getParaToInt("pageNumber");
		if (dept_id>0) {
			Page<User> list =User.dao.paginate(n, pagen, "select *","from user where dept_id=" + dept_id);
			map.put("roles", rolelist);
			map.put("list", list);
		}else {
			Page<User> list = User.dao.paginate(n, pagen, "select *", "from user");
			map.put("roles", rolelist);
			map.put("list", list);
		}
		listrol.add(map);
		renderJson(listrol);
	}
	
	//更改每一页显示的行数
		public void gghs(){
			pagen=getParaToInt("pagen");
			renderText("success");
		}
		
	//查找添加的部门是否存在
	public void selectdeptnames(){
		List<Department> depts=Department.dao.find("select * from dept where dept_name='"+getPara("str")+"'");
		if (depts.size()==0) {
			renderText("success");
		}
		else {
			renderText("false");
		}
	}

}
